/*
 * @Author: your name
 * @Date: 2021-05-30 22:18:58
 * @LastEditTime: 2021-07-27 17:41:56
 * @LastEditors: zhoulei0232
 * @Description: In User Settings Edit
 * @FilePath: \vue3-admin\src\router\index.ts
 */
import { createRouter, createWebHashHistory } from "vue-router";
import { store } from "@/store";
const ALL_PERMISSIONS = ["AllMenu"];

const routes = [
  {
    name: "home",
    path: "/",
    meta: {
      title: "首页",
      roles: [...ALL_PERMISSIONS],
    },
    component: () => import("@/views/Home/index.vue"),
    redirect: "/dashboard",
    children: [
      {
        name: "dashboard",
        path: "/dashboard",
        meta: {
          title: "欢迎页",
          roles: [...ALL_PERMISSIONS],
        },
        component: () => import("@/views/Demo1/Dashboard/index.vue"),
      },
      {
        name: "table",
        path: "/table",
        meta: {
          title: "table页面",
          roles: [...ALL_PERMISSIONS],
        },
        component: () => import("@/views/Demo2/Table/index.vue"),
      },
      {
        name: "tabs",
        path: "/tabs",
        meta: {
          title: "tabs页面",
          roles: [...ALL_PERMISSIONS],
        },
        component: () => import("@/views/Demo3/Tabs/index.vue"),
      },
      {
        name: "form",
        path: "/form",
        meta: {
          title: "form页面",
          roles: [...ALL_PERMISSIONS],
        },
        component: () => import("@/views/Demo4/Form/index.vue"),
      },
      {
        name: "rules",
        path: "/rules",
        meta: {
          title: "form页面",
          roles: [...ALL_PERMISSIONS],
        },
        component: () => import("@/views/ToolsPage/Rules/index.vue"),
      },

      {
        name: "permission",
        path: "/permission",
        meta: {
          title: "permission页面",
          roles: ["admin"],
        },
        component: () => import("@/views/Demo5/Permission/index.vue"),
      },
      {
        name: "404",
        path: "/404",
        meta: {
          title: "404页面",
          roles: [...ALL_PERMISSIONS],
        },
        component: () => import("@/views/ErrorPage/404.vue"),
      },
      {
        name: "403",
        path: "/403",
        meta: {
          title: "403页面",
          roles: [...ALL_PERMISSIONS],
        },
        component: () => import("@/views/ErrorPage/403.vue"),
      },
    ],
  },
  {
    name: "login",
    path: "/login",
    meta: {
      title: "欢迎页",
      roles: [...ALL_PERMISSIONS],
    },
    component: () => import("@/views/Login/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
const appStore = store;

router.beforeEach((to, from, next) => {
  let admin = appStore.state.appModule.userInfo.token;
  let role = appStore.state.appModule.userInfo.role;

  if (!admin && to.path !== "/login") {
    next({ path: "/login" });
  } else if (to.meta.roles) {
    let routerRole: any = to.meta.roles || [];
    let permissonFlag; // 权限判断
    if (routerRole.includes("AllMenu") || routerRole.includes(role)) {
      permissonFlag = true;
    } else {
      permissonFlag = false;
    }
    permissonFlag ? next() : next("/403");
  } else {
    next();
  }
});

export default router;
